package Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.mysql.jdbc.PreparedStatement;

public class GoodsSQL{
	String url = "jdbc:mysql://localhost:3306/goods";
    String userName = "root";
    String password = "kamisama";
    String driverName = "com.mysql.jdbc.Driver";
    String strSql = "insert into good(name,price,num,description) values(?,?,?,?)";
    Scanner sc = new Scanner(System.in);
    	Connection con = null;
    	PreparedStatement pstmt = null;
    	ResultSet rs = null;
    	String driverName1 = "com.mysql.jdbc.Driver";
    	
    public void Sell(String name,double price,int num,String description){                        //上架商品
    	try {
			Class.forName(driverName1);// 加载MySql的驱动程序
	        con = (Connection) DriverManager.getConnection(url, userName, password); // 根据参数的插入数据
	        pstmt = (PreparedStatement) con.prepareStatement(strSql);
	        pstmt.setString(1, name);
	        pstmt.setDouble(2,price);
	        pstmt.setInt(3, num);
	        pstmt.setString(4, description);
	        pstmt.executeUpdate();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e){
			e.printStackTrace();
		}finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (pstmt != null){
                try {
                    pstmt.close();// 关闭语句*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
